package com.dong;

import javafx.application.Application;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import javax.sql.DataSource;
import java.sql.Connection;

/**
 * HicariCP单数据源，HikariCP性能较好的数据库连接池，springboot2.x默认的数据库连接池
 * <p>
 * 运行信息：
 * : HikariPool-1 - Starting...
 * : HikariPool-1 - Start completed.
 * : 【run】【获取连接:HikariProxyConnection@1100109058 wrapping com.mysql.jdbc.JDBC4Connection@13e3c1c7】
 * : HikariPool-1 - Shutdown initiated...
 * : HikariPool-1 - Shutdown completed.
 *
 * @param null
 * @author DJ
 * @date 2023/8/2 16:28
 * @return null
 */
@SpringBootApplication
public class SpringBootHikaricpSingleApplication implements CommandLineRunner {

    private Logger logger = LoggerFactory.getLogger(Application.class);

    @Autowired
    DataSource dataSource;

    public static void main(String[] args) {
        SpringApplication.run(SpringBootHikaricpSingleApplication.class, args);
    }


    @Override
    public void run(String... args) throws Exception {
        try (Connection conn = dataSource.getConnection()) {
            // todo 其他操作...
            logger.info("【run】【获取连接:{}】", conn);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
